/**
 * @param {Array} array
 * @param {Number} size
 * @returns {Array}
 */
export const chunk = (array, size = 1) => {
  let newArray = [];
  for (let i = 0; i < array.length; i = i + size) {
    newArray.push(array.slice(i, i + size));
  }
  return newArray;
}

/**
 * @param {Array} array
 * @param {Number} depth
 * @returns {Array}
 */
export const flat = (array, depth = 1) => {
  if (depth > 0) {
    return array.reduce((a, c) => {
      const cur = (Array.isArray(c) || ArrayBuffer.isView(c)) ?
        flat(c, depth - 1)
        :
        [c];
      return [...a, ...cur]
    }, []);
  }
  return array.slice();
}

/**
 * 生成一个size大小的数组
 * @param {Number} size
 * @param {Function} func
 * @returns {Array}
 */
export const map = (size, func = () => { }) => {
  return Array(size).fill(0).map((_, i) => func(i));
}
